﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class GroupController : Controller
    {
        private GroupInfo groinfo = new GroupInfo(true);
        public ActionResult GroupIndex()
        {
            return View();
        }

        public ActionResult AddGroup()
        {
            ViewData["NodeID"] = GetNodeID();
            return View();
        }

        /// <summary>
        /// 获取食堂列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetNodeID()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = groinfo.GetNodeID();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["NodeID"]), ds.Tables[0].Rows[i]["Nodename"].ToString());
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString()
                }).ToList();
            return selectList;
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditGroup(string id)
        {
            ViewData["NodeID"] = GetNodeID();
            GroupVM vm = new GroupVM();
            GroupInfo info = new GroupInfo();
            groinfo = info.GetGroupInfoOne(id);
            vm.ID = groinfo.ID;
            vm.Node_ID = groinfo.Node_ID;
            vm.Group_Name = groinfo.Group_Name;
            vm.Remark = groinfo.Remark;
            Mapper.DynamicMap(groinfo, vm);
            return View(vm);
        }

        /// <summary>
        /// 获取班组列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetGroupList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                query = " and Group_Name like '%" + param[0] + "%'";
            }
            IList<GroupInfo> grolist = groinfo.SelGroupInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = groinfo._Groupcount;
            List<string> v;
            foreach (GroupInfo item in grolist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.Group_Name);
                v.Add(item.Remark);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 保存班组（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveGroup(GroupVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                GroupInfo gro = new GroupInfo()
                {
                    Group_Name = model.Group_Name,
                    Remark = model.Remark,
                    Node_ID = model.Node_ID
                };
                result = groinfo.SaveGroup(gro);
            }
            return Json(result);
        }

        /// <summary>
        /// 保存班组（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveGroupEdit(GroupVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                GroupInfo gro = new GroupInfo()
                {
                    ID = model.ID,
                    Node_ID = model.Node_ID,
                    Group_Name = model.Group_Name,
                    Remark = model.Remark
                };
                result = groinfo.SaveGroupEdit(gro);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteGroup(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = groinfo.DeleteGroup(strId);
            return Json(result);
        }

    }
}
